package mx.itesm.twritter.client;

import com.google.code.gwt.geolocation.client.Coordinates;
import com.google.code.gwt.geolocation.client.Geolocation;
import com.google.code.gwt.geolocation.client.Position;
import com.google.code.gwt.geolocation.client.PositionCallback;
import com.google.code.gwt.geolocation.client.PositionError;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.DockPanel;

import mx.itesm.twritter.shared.ToDraw;
import mx.itesm.twritter.shared.Tweet;
import mx.itesm.twritter.shared.map.Node;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ScrollPanel;

public class Twritter implements EntryPoint, ClickHandler {
	
	
	RootPanel rootPanel;
	DockPanel dockPanel;
	private Button btnClickMe = new Button("Parse 1!");
	private Button btnClickMe2 = new Button("Parse 2!");
	private VerticalPanel verticalPanel;
	protected TextArea TweetText;
	private TextArea parsedText;
	
	/** remote service **/
	private static final ParsingServiceAsync greetingService = GWT.create(ParsingService.class);
	private Button btnLocalzame;
	
	Coordinates posicion;
	private Label lblNewLabel_1;
	private Label label;
	private Label lblNewLabel_2;
	private TextArea processedTweet;
	private ScrollPanel scrollPanel;
	private Label lbl_cross;
	private TextArea crossings;

	public void onModuleLoad() {
		
		rootPanel = RootPanel.get("right"); 
		rootPanel.setSize("275px", "500px");
		
		dockPanel = new DockPanel();
		rootPanel.add(dockPanel);
		dockPanel.setSize("275px", "500px");

		final Label lblNewLabel = new Label("Consola de debuggeo");
		dockPanel.add(lblNewLabel, DockPanel.NORTH);
		
		scrollPanel = new ScrollPanel();
		dockPanel.add(scrollPanel, DockPanel.CENTER);
		scrollPanel.setSize("300px", "450px");

		verticalPanel = new VerticalPanel();
		scrollPanel.setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		verticalPanel.add(btnClickMe);
		btnClickMe.setWidth("86px");
		
		verticalPanel.add(btnClickMe2);
		btnClickMe2.setWidth("86px");
		
		lblNewLabel_1 = new Label("Tweet");
		verticalPanel.add(lblNewLabel_1);
		
		TweetText = new TextArea();
		TweetText.setStyleName("tarea");
		TweetText.setHeight("70px");
		verticalPanel.add(TweetText);
		
		label = new Label("Parsed 1");
		verticalPanel.add(label);
		
		parsedText = new TextArea();
		parsedText.setStyleName("tarea");
		parsedText.setHeight("80px");
		verticalPanel.add(parsedText);
		
		lblNewLabel_2 = new Label("Processed");
		verticalPanel.add(lblNewLabel_2);
		
		processedTweet = new TextArea();
		processedTweet.setStyleName("tarea");
		verticalPanel.add(processedTweet);
		
		lbl_cross = new Label("Crossings");
		verticalPanel.add(lbl_cross);
		
		crossings = new TextArea();
		crossings.setStyleName("tarea");
		verticalPanel.add(crossings);
		
		btnClickMe.addClickHandler(this);
		btnClickMe2.addClickHandler(this);
		
		btnLocalzame = new Button("Local\u00EDzame!");
		
		MapLayer.twitterConsole = this;
		
		/** sets the javascript callRPC method to the java method in MapLayer :) */
		MapLayer.exportMethods();

		if( Geolocation.isSupported() ) {
			verticalPanel.add(btnLocalzame);
			btnLocalzame.addClickHandler(this);
		}
	}

	@Override
	public void onClick(ClickEvent event) {

		Widget w = (Widget)event.getSource();
		
		if(w == btnClickMe) {
			Tweet tweet = new Tweet();
			tweet.setUser("FonzTraffic");
			tweet.setId("0");
			tweet.setText(TweetText.getText().trim());
			//Window.alert(tweet.getText());
			greetingService.parse(0, tweet, new AsyncCallback<ToDraw>() {
				
				@Override
				public void onSuccess(ToDraw result) {
					MapLayer.drawPoly(result.getRoute(), result.getVelocity(), result.getAutor(), result.getText(), result.getHora());
					parsedText.setText(result.getParsed() + "\n" + result.getVelocity());
					processedTweet.setText(result.getProcessed());
					crossings.setText(result.getCrossings());
				}
				
				@Override
				public void onFailure(Throwable caught) {
					Window.alert("Hubo un error al intenter contactar al servidor. " +
							"Por favor revisa tu conexion a internet");
				}
			});
		}
		
		if(w == btnClickMe2) {
			Tweet tweet = new Tweet();
			tweet.setUser("FonzTraffic");
			tweet.setId("0");
			tweet.setText(TweetText.getText().trim());
			
			greetingService.parse(1, tweet, new AsyncCallback<ToDraw>() {
				
				@Override
				public void onSuccess(ToDraw result) {
					MapLayer.drawPoly(result.getRoute(), result.getVelocity(), result.getAutor(), result.getText(), result.getHora());
					parsedText.setText(result.getParsed() + "\n" + result.getVelocity());
					processedTweet.setText(result.getProcessed());
					crossings.setText(result.getCrossings());
				}
				
				@Override
				public void onFailure(Throwable caught) {
					Window.alert("Hubo un error al intentar contactar al servidor. " +
							"Por favor revisa tu conexion a internet");
				}
			});
		}
		
		if(w == btnLocalzame) {
			final Geolocation geo = Geolocation.getGeolocation();	
			geo.getCurrentPosition(new PositionCallback() {
			    public void onFailure(PositionError error) {
			        // Handle failure
			    }
			    public void onSuccess(Position position) {
			        Coordinates coords = position.getCoords();
			        posicion=coords;					        
			        final Node miPosicion= new Node(posicion.getLatitude(), posicion.getLongitude());					        
			        MapLayer.drawMyPosition(miPosicion);
			    }
			});
		}
		
	}
		
	
}
